//
//  Meeting_Detail_In_PersonListViewController.m
//  HuaRun2
//
//  Created by admin on 17/9/4.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "Meeting_Detail_In_PersonListViewController.h"
#import "Meeting_Detail_In_PersonCell.h"
#import "TwoLabelView.h"
#import "Meeting_Detail_InPerson.h"
#import "Contact_DetailViewController.h"

@interface Meeting_Detail_In_PersonListViewController ()

@end

@implementation Meeting_Detail_In_PersonListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    
//    "meetings_member_in"= "内部与会人员";//
    self.navigationItem.title = NSLocalizedString(@"meetings_member_in", nil);
//    @"内部与会人员";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = RGBA(249, 249, 249, 1);
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = backItem;

    [self.tableView registerClass:[Meeting_Detail_In_PersonCell class] forCellReuseIdentifier:@"Meeting_Detail_In_PersonCell"];
    
    TwoLabelView * headerView = [TwoLabelView new];
    headerView.frame = CGRectMake(kMargin10, 0, kMainScreenWidth - 20, 60);
    
    headerView.nameLab.text = [NSString showSpaceStringWithStr:_name];
    headerView.companyLab.text = [NSString showSpaceStringWithStr: _companyName];
    headerView.companyLab.hidden= YES;
    self.tableView.tableHeaderView = headerView;

    [self dataInit];
    [self methodBind];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)methodBind {
    weakself(ws);
    [self.tableView headerRefresh:^{
        [ws refreshData];
    }];
    
}
- (void)dataInit {
    self.current = 1;
    self.size = 10;
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Meeting_Detail_In_PersonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Meeting_Detail_In_PersonCell" forIndexPath:indexPath];
    
    cell.inPersonModel = _dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    Contact_DetailViewController * meetingVC = [[Contact_DetailViewController alloc] init];
    meetingVC.hidesBottomBarWhenPushed = YES;
    Meeting_Detail_InPerson * personModel = _dataArray[indexPath.row];
    meetingVC.contactId = personModel.id;
    [self.navigationController pushViewController:meetingVC animated:YES];

}

//获取内部与会人员个数
- (void)fetchInContactPersonNum {
    
    NSString * url = [NSString stringWithFormat:@"%@%@", kBaseUrl, kMeeting_Detail_In_PersonListUrl];
    NSDictionary * dic = @{@"meetingId": _meetingId,
                           @"current": @(_current)};
    
    weakself(ws);
    [NetWorkHelper getWithURLString:url parameters:dic success:^(id object) {
        [ws.tableView endHeaderRefresh];

        if (![object[@"code"] isEqual: kSuccessCode]) {
            NSString * info = [NSString isemptyStr:object[@"msg"]] ? kUnknownError : object[@"msg"];
            [ws showWithText:info];
            return;
        }
        
        NSArray * addressArray = [NSArray yy_modelArrayWithClass:[Meeting_Detail_InPerson class]
                                                            json:object[@"list"]];
        
        ws.dataArray = ws.current == 1 ? addressArray : [ws.dataArray arrayByAddingObjectsFromArray:addressArray];
        [ws.tableView reloadData];
        
        if (addressArray.count >= ws.size) {
            [ws.tableView footerRefresh:^{
                [ws loadMoreData];
            }];
        }else {
            [ws.tableView hiddenFooter];
        }

    } failure:^(NSError *error) {
        
        [ws.tableView endHeaderRefresh];
        [ws showWithText:kNetworkError];
    }];
    
}
- (void)refreshData {
    self.current = 1;
    [self fetchInContactPersonNum];
}

- (void)loadMoreData {
    self.current++;
    [self fetchInContactPersonNum];
}


@end
